package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.LinkedList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import componenti.statistica.CoppiaIperesponenziale;
import componenti.statistica.Esponenziale;

@SuppressWarnings("serial")
public class DialogAggiungiFunzione extends JDialog {

	JPanel p = new JPanel();
	JPanel p1 = new JPanel();
	ButtonGroup group;
	JFormattedTextField lambdaTx;
	JFormattedTextField alfaTx;
	LinkedList<CoppiaIperesponenziale> funzioni;
	PCPanel panel;
	

	public DialogAggiungiFunzione(PCPanel panel, LinkedList<CoppiaIperesponenziale> funzioni,
			JDialog f) {
		super(f);
		setTitle("Aggiungi funzione");
		this.funzioni = funzioni;
		this.panel = panel;
		setResizable(false);
		setModal(true);
		this.setLayout(new BorderLayout());
		p.setLayout(new GridBagLayout());
		init();
		initButtons();
		add(p, BorderLayout.CENTER);
		add(p1, BorderLayout.SOUTH);
		p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
		pack();
		this.setLocationRelativeTo(f);
		setVisible(true);
	}

	private void initButtons() {
		JButton ok = new JButton("OK");
		ok.setAction(new OKAction());
		JButton annulla = new JButton("Annulla");
		annulla.setAction(new AnnullaAction());
		p1.add(ok);
		this.getRootPane().setDefaultButton(ok);
		p1.add(annulla);
	}

	private class AnnullaAction extends AbstractAction {

		public AnnullaAction() {
			super("Annulla");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			DialogAggiungiFunzione.this.dispose();
		}

	}

	private class OKAction extends AbstractAction {

		public OKAction() {
			super("OK");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			Double inputLambda = Double.parseDouble(sostituisciVirgola(lambdaTx.getText()));
			Double inputAlfa = Double.parseDouble(sostituisciVirgola(alfaTx.getText()));
			
			if (inputLambda.equals("") || inputAlfa.equals("")) {
				JOptionPane.showMessageDialog(DialogAggiungiFunzione.this,
						"Non puoi aggiungere una funzione senza parametri",
						"Errore", JOptionPane.ERROR_MESSAGE);

			} else {
				panel.setSomma(panel.getSomma() + inputAlfa);
				if (panel.getSomma() > 1) {
					JOptionPane.showMessageDialog(DialogAggiungiFunzione.this,
							"Somma alfa maggiore di 1", "Errore",
							JOptionPane.ERROR_MESSAGE);
					panel.setSomma(panel.getSomma()
							- inputAlfa);

				} else {
//					String result = inputLambda + "-" + inputAlfa;
					CoppiaIperesponenziale c=new CoppiaIperesponenziale(inputAlfa, new Esponenziale(inputLambda));
					funzioni.add(c);
					dispose();
				}
			}
		}
	}

	private void init() {

		JLabel lambda = new JLabel("lambda");
		lambdaTx = new JFormattedTextField(new RegexFormatter("\\d+((.\\d){1}\\d?)?"));

		JLabel alfa = new JLabel("alfa");
		alfaTx = new JFormattedTextField(new RegexFormatter("\\d+((.\\d){1}\\d?)?"));

		p.add(lambda, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		p.add(lambdaTx, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		p.add(alfa, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		p.add(alfaTx, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
	}
	
	private String sostituisciVirgola(String s) {
		char[] x = s.toCharArray();
		for (int i = 0; i < x.length; i++) {
			if (x[i] == ',') {
				x[i] = '.';
			}
		}
		return new String(x);
	}
}
